<?php
namespace app\api\controller;

use app\model\Createroom as Room; 

class CreateRoom extends Baseic
{
    public function __auto()
    {
        $this->model = new Room;
    }

    // 创建房间 -- 阿卡索接口
    public function create()
    {   
        $object = new Live();

        $data['name'] = input('title');
        
        $data['teacher_pass'] = substr(md5(rand(0,555).uniqid()), 20);
        $data['student_pass'] = substr(md5(rand(0,555).uniqid()), 20);
        $data['classUrl_teacher'] = $object->getLiveUrl()['push_url'];
        $data['classUrl_student'] = $object->getPlayUrl($object->getLiveUrl()['stream_id']);
        $data['admin_id'] = session('admin_user_id');
        $data['start_time'] = strtotime($data['sel1'].'-'.$data['sel2'].'-'.$data['sel3'].' '.$data['sel4'].':00');
        $data['end_time'] = $data['start_time'] + 60*60;
        $data['createTime'] = time();
        $data['title'] = '客户端定义';
        
        if($data['createTime'] == $data['start_time'] || $data['createTime'] > $data['start_time']){
            $this->Retterror(config('Notice.timeError'));//创建时间有误
            return $this->retResponse();
        }

        if(empty($data['tid']) || $data['tid'] == ''){
            $this->Retterror(config('Notice.classError'));//教材不能为空
            return $this->retResponse();
        }

        $res = $this->model->createRoom($data);
        if($res){
            $teacher = [
                    'role'  =>  'teacher',
                    'pwd'   =>   $data['teacher_pass'],
            ];
            $student = [
                    'role'  =>  'student',
                    'pwd'   =>   $data['student_pass'],
            ];
            $this->data = [
                'class_key' => '',
                'class_pwd' => [$teacher,$student],
            ];
            $this->Rettsuccess();
            return $this->retResponse();
        }else{
            $this->Retterror();
            return $this->retResponse();
        }
    }


    //获取课堂出席详情 -- 阿卡索接口
    # class_key 课堂id
    public function GetClassDetail()
    {   
        if ($this->request->isPost()) {
            $cid = input('class_key');

            $this->data = $this->model->getClassdetail($id);
            if($this-data){
                $this->Rettsuccess();
                return $this->retResponse();
            }else{
                $this->Retterror();
                return $this->retResponse();
            }
        }
    }


    // 获取课后报告 -- 阿卡索接口
    # class_key 教室id 
    # report_module 模块  attend出席数据 video录像查看 pandect总览数据
    public function GetReportModule()
    {
        $id = input('class_key');
        $report = input('report_module');

        $this->data = $this->model->getreport($id,$report);

        if($this->data){
            $this->Rettsuccess();
            return $this->retResponse();
        }else{
            $this->Retterror();
            return $this->retResponse();
        }
    }

    // 获取登录时长数据 -- 阿卡索接口
    # class_key 教师id
    public function getChargeFromAcadsoc()
    {
        $id = input('class_id');
        $this->data = $this->model->getCharge($id);
        if($this->data){
            $this->Rettsuccess();
            return $this->retResponse();
        }else{
            $this->Retterror();
            return $this->retResponse();
        }
    }
}   